<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/29
 * Time: 17:27
 */

namespace app\models\weixin;

use Yii;
use app\lib\BaseModel;
//场景
class T_WXMALL_SCENE_USER extends BaseModel
{
    //扫描场景添加信息
    public static function setSCENE_USER($openid, $SCENE_ID, $SCENE_TYPE, $STATUS){
        if(static::getSCENE_USER_count($openid,$SCENE_ID)>0){
            //已经添加
            return 1;
        }
        $id=static::uuid();
        $CREATE_DATETIME=static::getDateTimeStr();
        $command = static::query()->createCommand('insert into T_WXMALL_SCENE_USER (ID,WX_OPENID,SCENE_ID,SCENE_TYPE,CREATE_DATETIME,STATUS)
          values (:ID,:WX_OPENID,:SCENE_ID,:SCENE_TYPE,:CREATE_DATETIME,:STATUS)');
        $command->bindValue(':ID',$id);
        $command->bindValue(':WX_OPENID',$openid);
        $command->bindValue(':SCENE_ID',$SCENE_ID);
        $command->bindValue(':SCENE_TYPE',$SCENE_TYPE);
        $command->bindValue(':CREATE_DATETIME',$CREATE_DATETIME);
        $command->bindValue(':STATUS',$STATUS);


        return $command->execute();
    }

    //判断是否已经添加了记录
    //扫描场景添加信息
    public static function getSCENE_USER_count($openid, $SCENE_ID){

        $command = static::query()->createCommand('select count(1) from  T_WXMALL_SCENE_USER
           where WX_OPENID=:WX_OPENID and SCENE_ID=:SCENE_ID');
        $command->bindValue(':WX_OPENID',$openid);
        $command->bindValue(':SCENE_ID',$SCENE_ID);

//        return 1;
        return $command->queryScalar();
    }

    //判断是否有优惠权限
    public static function getSCENE_USER_isValid_count($openid, $SCENE_ID){

        $command = static::query()->createCommand('select count(1) from  T_WXMALL_SCENE_USER
           where WX_OPENID=:WX_OPENID and SCENE_ID=:SCENE_ID and STATUS=1');
        $command->bindValue(':WX_OPENID',$openid);
        $command->bindValue(':SCENE_ID',$SCENE_ID);

//        return 1;
        return $command->queryScalar();
    }

    //设置优惠权限 为无效
    public static function setSCENE_USER_invalid($openid, $SCENE_ID){

        $command = static::query()->createCommand('update  T_WXMALL_SCENE_USER set STATUS=0
           where WX_OPENID=:WX_OPENID and SCENE_ID=:SCENE_ID ');
        $command->bindValue(':WX_OPENID',$openid);
        $command->bindValue(':SCENE_ID',$SCENE_ID);

        return $command->execute();
    }
}